<!doctype html>
<html>
<head>
  <meta charset='utf-8'>
  <title>Load &amp; Save (Ajax) - Handsontable</title>

  <!--
  Loading Handsontable (full distribution that includes all dependencies apart from jQuery)
  -->
  <script src="../lib/jquery.min.js"></script>
  <script src="../dist/jquery.handsontable.full.js"></script>
  <link rel="stylesheet" media="screen" href="../dist/jquery.handsontable.full.css">

  <!--
  Loading demo dependencies. They are used here only to enhance the examples on this page
  -->
  <link rel="stylesheet" media="screen" href="css/samples.css?20140331">
  <script src="js/samples.js"></script>
  <script src="js/highlight/highlight.pack.js"></script>
  <link rel="stylesheet" media="screen" href="js/highlight/styles/github.css">
  <link rel="stylesheet" href="css/font-awesome/css/font-awesome.min.css">

  <!--
  Facebook open graph. Don't copy this to your project :)
  -->
  <meta property="og:title" content="Load &amp; Save (Ajax)">
  <meta property="og:description"
        content="Use the onChange callback to track changes made in the table. In the example below, $.ajax is used to load and save grid data.">
  <meta property="og:url" content="http://handsontable.com/demo/ajax.html">
  <meta property="og:image" content="http://handsontable.com/demo/image/og-image.png">
  <meta property="og:image:type" content="image/png">
  <meta property="og:image:width" content="409">
  <meta property="og:image:height" content="164">
  <link rel="canonical" href="http://handsontable.com/demo/ajax.html">

  <!--
  Google Analytics for GitHub Page. Don't copy this to your project :)
  -->
  <script src="js/ga.js"></script>
</head>

<body>


<div class="wrapper">
<div class="wrapper-row">
<div id="global-menu-clone">
  <h1><a href="../index.html">Handsontable</a></h1>

</div>

<div id="container">
  <div class="columnLayout">

        <div class="rowLayout">
          <div class="descLayout">
            <div class="pad">
              <h2>Load &amp; Save (Ajax)</h2>

              <p>Use the
                <b>onChange</b> callback to track changes made in the table. In the example below, $.ajax is used to load
                and save grid data.
              </p>

              <p>Note: this is a mockup. Nothing is acually saved. You have to implement that part server-side.</p>

              <p>
                <button name="load">Load</button>
                <button name="save">Save</button>
                <label><input type="checkbox" name="autosave" checked="checked" autocomplete="off"> Autosave</label>
              </p>

              <pre id="example1console" class="console">Click "Load" to load data from server</pre>

              <div id="example1"></div>

              <p>
                <button name="dump" data-dump="#example1" title="Prints current data source to Firebug/Chrome Dev Tools">
                  Dump data to console
                </button>
              </p>
            </div>
          </div>

          <div class="codeLayout">
            <div class="pad">
              <script>
                var $container = $("#example1");
                var $console = $("#example1console");
                var $parent = $container.parent();
                var autosaveNotification;
                $container.handsontable({
                  startRows: 8,
                  startCols: 6,
                  rowHeaders: true,
                  colHeaders: true,
                  minSpareRows: 1,
                  contextMenu: true,
                  afterChange: function (change, source) {
                    if (source === 'loadData') {
                      return; //don't save this change
                    }
                    if ($parent.find('input[name=autosave]').is(':checked')) {
                      clearTimeout(autosaveNotification);
                      $.ajax({
                        url: "json/save.json",
                        dataType: "json",
                        type: "POST",
                        data: JSON.stringify({data: change}), //contains changed cells' data
                        complete: function (data) {
                          $console.text('Autosaved (' + change.length + ' ' +
                            'cell' + (change.length > 1 ? 's' : '') + ')');
                          autosaveNotification = setTimeout(function () {
                            $console.text('Changes will be autosaved');
                          }, 1000);
                        }
                      });
                    }
                  }
                });
                var handsontable = $container.data('handsontable');

                $parent.find('button[name=load]').click(function () {
                  $.ajax({
                    url: "json/load.json",
                    dataType: 'json',
                    type: 'GET',
                    success: function (res) {
                      handsontable.loadData(res.data);
                      $console.text('Data loaded');
                    }
                  });
                });

                $parent.find('button[name=save]').click(function () {
                  $.ajax({
                    url: "json/save.json",
                    data: JSON.stringify({"data": handsontable.getData()}), //returns all cells' data
                    dataType: 'json',
                    type: 'POST',
                    success: function (res) {
                      if (res.result === 'ok') {
                        $console.text('Data saved');
                      }
                      else {
                        $console.text('Save error');
                      }
                    },
                    error: function () {
                      $console.text('Save error. POST method is not allowed on GitHub Pages. ' +
                        'Run this example on your own server to see the success message.');
                    }
                  });
                });

                $parent.find('input[name=autosave]').click(function () {
                  if ($(this).is(':checked')) {
                    $console.text('Changes will be autosaved');
                  }
                  else {
                    $console.text('Changes will not be autosaved');
                  }
                });
              </script>
            </div>
          </div>
        </div>

    <div class="footer-text">Handsontable &copy; 2012-2014 Marcin Warpechowski and contributors.<br> Code and documentation
      licensed under the The MIT License.
    </div>
  </div>

</div>

</div>
</div>

<div id="outside-links-wrapper"></div>

</body>
</html>
